/**
 * @author Morten J, Morten B & Jonas
 */
package control;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JLabel;
import view.GUI;

public class DBHandler {

    private static Connection conn;
    private static Statement stmt;
    private boolean dbSucces;
    private String db;

    public DBHandler(String host, String pnr, String db, String usr, String pwd, JLabel label) {
        dbSucces = false;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://" + host + ":" + pnr + "/" + db;
            conn = DriverManager.getConnection(url, usr, pwd);
            stmt = conn.createStatement();
            this.db = db;
        } catch (SQLException ex) {
            String errMsg = "Ukendt fejl " + ex.getErrorCode() + ", prøv igen senere";;
            if (ex.getErrorCode() == 0) {
                errMsg = "Forbindelsen blev ikke fundet, kontroller at 'Host' og 'Port' er korrekt";
            } else if (ex.getErrorCode() == 1045) {
                errMsg = "Brugernavn og Password stemmer ikke";
            } else if (ex.getErrorCode() == 1049) {
                errMsg = "Databasen kunne ikke findes";
            }
            label.setText(errMsg);
        } catch (ClassNotFoundException ex) {
            label.setText("Fejl i driver til databasen, kontakt din programmør");
        }
    }

    public boolean isConnected() {
        boolean retur = false;
        if (stmt != null) {
            retur = true;
        }
        return retur;
    }

    /**
     * Select-metoden bør blot returnere et resultset som vi så skal bearbejde
     * med en while(rs.next) i den klasse man kalder den i.
     *
     * @param sql
     * @return resultset fra execution af den kaldte SQL-streng.
     * @throws java.sql.SQLException
     */
    public ResultSet select(String sql) throws SQLException {
        dbSucces = false;
        ResultSet rs = null;
        if (stmt != null) {
            rs = stmt.executeQuery(sql);
            dbSucces = true;
        }
        return rs;
    }

    public void insert(String sql) throws SQLException {
        dbSucces = false;
        stmt.execute(sql);
        dbSucces = true;
    }

    public boolean isDbSucces() {
        return dbSucces;
    }

    public void update(String sql) throws SQLException {
        stmt.execute(sql);
    }

    public int getNextInvoiceNumber() throws SQLException, NullPointerException {
        int nextInvoiceNumber = 0;
        String tabel = "faktura";

        String sql = "select AUTO_INCREMENT as AI from INFORMATION_SCHEMA.TABLES "
                + "where TABLE_SCHEMA = '" + db + "' "
                + "and TABLE_NAME = '" + tabel + "'";
        ResultSet rs = select(sql);

        rs.next();
        nextInvoiceNumber = rs.getInt("AI");
        return nextInvoiceNumber;
    }

    public void delete(String sql) throws SQLException {
        stmt.execute(sql);
    }

    public void closeConn() {
        try {
            conn.close();
        } catch (SQLException ex) {
            GUI.getGuiHandler().setFeedbackMessage("");
        }
    }
}
